//
//  MyHandle.m
//  RedManStock
//
//  Created by lanou3g on 15/7/18.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "MyHandle.h"
#import "MBProgressHUD.h"
@implementation MyHandle

//static MyHandle *myHandle;
//+ (MyHandle *)shareHandle{
//    if (myHandle==nil) {
//        myHandle=[[MyHandle alloc]init];
//    }
//    return myHandle;
//}

+ (CGFloat)stringHeight:(NSString *)str
               fontSize:(CGFloat)fontSize
                  width:(CGFloat)width{
    NSDictionary *dic=@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]};
    CGRect rect=[str boundingRectWithSize:CGSizeMake(width,1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect.size.height;
}

+ (void)setUpMBProgressWithView:(UIView *)view
                           Text:(NSString *)text{
    MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.mode=MBProgressHUDModeText;
    hud.labelText=text;
    [hud hide:YES afterDelay:1.5f];
}

+ (NSDateComponents *)getNowDetailDate{
    NSDate *date=[NSDate date];
    NSCalendar *calendar=[NSCalendar currentCalendar];
    NSInteger flag=NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitHour |NSCalendarUnitMinute | NSCalendarUnitSecond;
    //这样给出的时间是你当地的时间
    NSDateComponents *time=[calendar components:flag fromDate:date];
    //    NSArray *weekArray=[NSArray arrayWithObjects:[NSNull null],@"星期日",@"星期一",@"星期二",@"星期三",@"星期四",@"星期五",@"星期六", nil];
    //    NSLog(@"%lu %lu %lu %lu %lu,%@",time.year,time.month,time.day,time.hour,time.minute,weekArray[time.weekday]);
    
    return time;
}

- (void)requestDataWithURLStr:(NSString *)URLStr parameterBlock:(requestParameter)parameterBlock successBlock:(requestDicSuccess)successBlock errorBlock:(requestDicError)errorBlock{
    
    self.parameterBlock=parameterBlock;
    self.successBlock=successBlock;
    self.errorBlock=errorBlock;
    
//    NSString *requestURL=[URLStr componentsSeparatedByString:@"?"][0];
    
    
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    
    //必须加这段
    manager.responseSerializer.acceptableContentTypes=[manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
    
    NSDictionary *dic=self.parameterBlock();
    
    //带POST参数的请求
    [manager POST:URLStr parameters:dic success:^(AFHTTPRequestOperation *operation, NSDictionary *responseDic) {
        
        self.successBlock(responseDic);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        self.errorBlock(error);
        
    }];
    
    
}



@end
